/*	BuildControlTower class definition.
 *
*/

#include "BuildingPlacer.h"
#include "BuildControlTower.h"

void BuildControlTower::Enter()
{
	DEBUG_PRINT("BuildControlTower(%p) %s entering\n", this, GetName().c_str());

}

void BuildControlTower::Execute()
{
	DEBUG_PRINT("BuildControlTower(%p) %s executing\n", this, GetName().c_str());
	std::list<Base*>* bases = BB->GetBases();
	std::list<BWAPI::Unit*>* starPorts;
	BWAPI::Unit* star = NULL;
	for(std::list<Base*>::iterator base = bases->begin(); base != bases->end() && !star; base++)
	{
		starPorts = (*base)->GetBuildingsByType(BWAPI::UnitTypes::Terran_Starport);
		for(std::list<BWAPI::Unit*>::iterator it = starPorts->begin(); it != starPorts->end(); it++)
		{
			if(!(*it)->getAddon())
			{
				star = (*it);
				break;
			}
		}
	}
	if(star)
	{
		star->buildAddon(BWAPI::UnitTypes::Terran_Control_Tower);
		BB->SpendResources(BWAPI::UnitTypes::Terran_Control_Tower);
	}
	control->FinishWithSuccess();
}

void BuildControlTower::Exit()
{
	DEBUG_PRINT("BuildControlTower(%p) %s exiting\n", this, GetName().c_str());
	
}

bool BuildControlTower::CheckConditions()
{
	DEBUG_PRINT("BuildControlTower(%p) %s checking conditions\n", this, GetName().c_str());
	
	std::list<BWAPI::Unit*>* star = BB->GetBuildingsByTypeGeneral(BWAPI::UnitTypes::Terran_Starport);
	std::list<BWAPI::Unit*>* tower = BB->GetBuildingsByTypeGeneral(BWAPI::UnitTypes::Terran_Control_Tower);
	if(star)
	{
		if(!tower)
		{
			if(BB->isBuildBuilding(BWAPI::UnitTypes::Terran_Control_Tower) )
			{
				DEBUG_PRINT("BuildControlTower(%p) %s checked conditions and has %d minerals to spend\n", this, GetName().c_str(), BB->GetMineralsToSpend());
				return true;
			}
		}
	}
	

	return false;
}